package com.hmy.uaa.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hmy.uaa.bo.request.RoleMenuRequest;
import com.hmy.uaa.bo.response.RoleMenuResponse;
import com.hmy.uaa.common.security.UserOAuth2Details;
import com.hmy.uaa.mapper.RoleMenuRelationMapper;
import com.hmy.uaa.po.RoleMenuRelation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class RoleMenuRelationService {


    @Autowired
    private RoleMenuRelationMapper roleMenuRelationMapper;


    /**
     * 查询用户
     *
     * @param roleId 角色id
     * @return List<RoleMenuResponse>  返回数据
     */
    public List<RoleMenuResponse> queryRoleMenuList(Long roleId) {


        //如果不是系统管理员，则不查询权限菜单
        List<String> roleList = UserOAuth2Details.getUserDTO().getRoles();
        Integer hidden = null;
        if (!roleList.get(0).equals("admin")) {
            hidden = Integer.valueOf(1);
        }
        return roleMenuRelationMapper.queryRoleMenuList(roleId, hidden);
    }

    /**
     * 判断菜单是否有绑定角色
     *
     * @param roleMenuRelation 查询参数
     * @return boolean true：有绑定；false：未绑定
     */
    public boolean isBindMenu(RoleMenuRelation roleMenuRelation) {


        //判断该菜单是否有绑定角色
        LambdaQueryWrapper<RoleMenuRelation> roleMenuWrapper = Wrappers.<RoleMenuRelation>lambdaQuery();

        if (Objects.nonNull(roleMenuRelation.getMenuId())) {
            roleMenuWrapper.eq(RoleMenuRelation::getMenuId, roleMenuRelation.getMenuId());
        }

        if (Objects.nonNull(roleMenuRelation.getRoleId())) {
            roleMenuWrapper.eq(RoleMenuRelation::getRoleId, roleMenuRelation.getRoleId());
        }
        return roleMenuRelationMapper.selectCount(roleMenuWrapper) > 0;
    }

    /**
     * 绑定菜单
     *
     * @param roleMenuRequest 绑定参数
     */
    @Transactional
    public void bindMenu(RoleMenuRequest roleMenuRequest) {

        //1、先删除绑定角色的菜单
        LambdaQueryWrapper<RoleMenuRelation> queryWrapper = Wrappers.<RoleMenuRelation>lambdaQuery()
                .eq(RoleMenuRelation::getRoleId, roleMenuRequest.getRoleId());
        roleMenuRelationMapper.delete(queryWrapper);

        //2、绑定菜单
        for (int i = 0; i < roleMenuRequest.getMenuIds().length; i++) {
            Long menuId = roleMenuRequest.getMenuIds()[i];
            if (Objects.nonNull(menuId)) {
                RoleMenuRelation roleMenuRelation = new RoleMenuRelation();
                roleMenuRelation.setRoleId(roleMenuRequest.getRoleId());
                roleMenuRelation.setMenuId(menuId);
                roleMenuRelation.setCreateTime(LocalDateTime.now());
                roleMenuRelationMapper.insert(roleMenuRelation);

            }
        }
    }


}
